home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs17.zoo / stdstrea.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-02  |  4.1 KB  |  128 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <ioprivat.h>
  19. #include <g_config.h>
  20.  
  21. // The ANSI draft requires that operations on cin/cout/cerr can be
  22. // mixed with operations on stdin/stdout/stderr on a character by
  23. // character basis.  This normally requires that the streambuf's
  24. // used by cin/cout/cerr be stdiostreams.  However, if the stdio
  25. // implementation is the one that is built using this library,
  26. // then we don't need to, since in that case stdin/stdout/stderr
  27. // are identical to &__std_filebuf_0/&__std_filebuf_1/&__std_filebuf_2.
  28.  
  29. #ifdef _STDIO_USES_IOSTREAM
  30. #define USE_FILEBUF
  31. #endif
  32.  
  33. #if defined(NAMES_HAVE_UNDERSCORE) || defined(_G_NAMES_HAVE_UNDERSCORE)
  34. #define UNDERSCORE "_"
  35. #else
  36. #define UNDERSCORE ""
  37. #endif
  38.  
  39. #ifdef USE_FILEBUF
  40. #define CIN_SBUF __std_filebuf_0
  41. #define COUT_SBUF __std_filebuf_1
  42. #define CERR_SBUF __std_filebuf_2
  43. static int use_stdiobuf = 0;
  44. #else
  45. #define CIN_SBUF __stdin_stdiobuf
  46. #define COUT_SBUF __stdout_stdiobuf
  47. #define CERR_SBUF __stderr_stdiobuf
  48. static int use_stdiobuf = 1;
  49. #endif
  50.  
  51. struct _fake_filebuf;
  52. extern _fake_filebuf __std_filebuf_0, __std_filebuf_1, __std_filebuf_2;
  53. struct _fake_stdiobuf;
  54. extern _fake_stdiobuf __stdin_stdiobuf, __stdout_stdiobuf, __stderr_stdiobuf;
  55.  
  56. #define cin CIN
  57. #define cout COUT
  58. #define cerr CERR
  59. #define clog CLOG
  60. #include <iostream.h>
  61. #undef cin
  62. #undef cout
  63. #undef cerr
  64. #undef clog
  65.  
  66. #ifdef __GNUG__
  67. #define PAD 0 /* g++ allows 0-length arrays. */
  68. #else
  69. #define PAD 1
  70. #endif
  71. struct _fake_istream {
  72.     struct myfields {
  73.     _ios_fields *vb; /* pointer to virtual base class ios */
  74.     _G_ssize_t _gcount;
  75.     } mine;
  76.     _ios_fields base;
  77.     char filler[sizeof(struct istream)-sizeof(struct _ios_fields)+PAD];
  78. };
  79. struct _fake_ostream {
  80.     struct myfields {
  81.     _ios_fields *vb; /* pointer to virtual base class ios */
  82.     } mine;
  83.     _ios_fields base;
  84.     char filler[sizeof(struct ostream)-sizeof(struct _ios_fields)+PAD];
  85. };
  86.  
  87. #define STD_STR(SBUF, TIE, EXTRA_FLAGS) \
  88.  (streambuf*)&SBUF, TIE, 0, ios::dont_close|ios::skipws|EXTRA_FLAGS, ' ',0,0,6
  89.  
  90. #define STREAM_DEF(TYPE, NAME, SBUF, TIE, EXTRA_FLAGS) \
  91.   TYPE NAME = { {&NAME.base}, {STD_STR(SBUF, TIE, EXTRA_FLAGS) }};
  92.  
  93. STREAM_DEF(_fake_ostream, cout, COUT_SBUF, NULL, 0)
  94. STREAM_DEF(_fake_ostream, cerr, CERR_SBUF, (ostream*)&cout, ios::unitbuf)
  95. STREAM_DEF(_fake_istream, cin, CIN_SBUF,  (ostream*)&cout, 0)
  96.  
  97. /* Only for (partial) compatibility with AT&T's library. */
  98. STREAM_DEF(_fake_ostream, clog, CERR_SBUF, (ostream*)&cout, 0)
  99.  
  100. // Switches between using __std_filebuf_{0,1,2} and
  101. // __std{in,out,err}_stdiobuf for standard streams.  This is
  102. // normally not needed, but is provided for AT&T compatibility.
  103.  
  104. int ios::sync_with_stdio(int new_state)
  105. {
  106. #ifdef _STDIO_USES_IOSTREAM
  107.     // It is always synced.
  108.     return 0;
  109. #else
  110.     if (new_state == use_stdiobuf) // The usual case now.
  111.     return use_stdiobuf;
  112.     if (new_state) {
  113.     cout.base._strbuf = (streambuf*)&__stdout_stdiobuf;
  114.     cin.base._strbuf = (streambuf*)&__stdin_stdiobuf;
  115.     cerr.base._strbuf = (streambuf*)&__stderr_stdiobuf;
  116.     clog.base._strbuf = (streambuf*)&__stderr_stdiobuf;
  117.     } else {
  118.     cout.base._strbuf = (streambuf*)&__std_filebuf_1;
  119.     cin.base._strbuf = (streambuf*)&__std_filebuf_0;
  120.     cerr.base._strbuf = (streambuf*)&__std_filebuf_2;
  121.     clog.base._strbuf = (streambuf*)&__std_filebuf_2;
  122.     }
  123.     int old_state = use_stdiobuf;
  124.     use_stdiobuf = new_state;
  125.     return old_state;
  126. #endif
  127. }
  128.